package com.hz.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hz.web.dao.LinkDao;
import com.hz.web.entity.LinkEntity;
import com.hz.web.service.IlinkService;
import com.hz.web.util.Uid;
@Service
public class LinkImpl implements IlinkService {

	@Resource
	private LinkDao linkDao;

	/**
	 * 添加数据
	 * @param entity
	 * @return
	 */
	@Override
	public int addLink(LinkEntity entity) {
		int row = -1;
		try {
			if(entity!=null){
				entity.setLinkId("linkid"+Uid.getUid());//主键
				row = linkDao.addLink(entity);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return row;
	}

	/**
	 * 查询链接地址数据
	 * @param parmsMap
	 * @return
	 */
	@Override
	public List<LinkEntity> queryLink(LinkEntity entity, Integer page,
			Integer rows) {
		Map<String, Object> parmsMap = new HashMap<String, Object>();
		if(entity!=null){
			String linkName  = entity.getLinkName();
			parmsMap.put("linkId", entity.getLinkId());
			parmsMap.put("linkName",linkName==null?"":"%"+linkName.trim()+"%" );
			parmsMap.put("linkUrl", entity.getLinkUrl());
			parmsMap.put("linkType", entity.getLinkType());
			parmsMap.put("isDel", entity.getIsDel());
			parmsMap.put("imgTextType", entity.getImgTextType());
	
		}
		if(page!=null && rows!=null && page!=0 && rows!=0 ){
			int index = (page-1)*rows;
			int end =rows; 
			parmsMap.put("index", index);
			parmsMap.put("end", end);
		}
		List<LinkEntity> ls =new ArrayList<LinkEntity>();
		try {
			ls=linkDao.queryLink(parmsMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ls;
	}

	/**
	 * 查询链接地址数据总条数
	 * @param parmsMap
	 * @return
	 */
	@Override
	public int queryLinkCount(LinkEntity entity) {
		Map<String, Object> parmsMap = new HashMap<String, Object>();
		int count = -1;
		if(entity!=null){
			String linkName  = entity.getLinkName();
			parmsMap.put("linkId", entity.getLinkId());
			parmsMap.put("linkName",linkName==null?"":"%"+linkName.trim()+"%" );
			parmsMap.put("linkUrl", entity.getLinkUrl());
			parmsMap.put("linkType", entity.getLinkType());
			parmsMap.put("isDel", entity.getIsDel());
			parmsMap.put("imgTextType", entity.getImgTextType());
	
		}
		try {
			count = linkDao.queryLinkCount(parmsMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

	/**
	 * 修改数据
	 * @param entity
	 * @return
	 */
	@Override
	public int updateLink(LinkEntity entity) {
		int row = -1;
		if(entity!=null && entity.getLinkId()!=null && !"".equals(entity.getLinkId().trim())){
			row = linkDao.updateLink(entity);
		}
		return row;
	}
}
